package com.medical.device.common;

import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yy
 */
public class R extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    public R() {
        put("code", 200);
        put("message", "success");
    }

    public static R error() {
        return error(0, "未知异常，请联系管理员");
    }

    public static R error(String msg) {
        return error(0, msg);
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("message", msg);
        return r;
    }

    public static <T> R error(String msg, T t) {
        R r = new R();
        r.put("code", 0);
        r.put("message", msg);
        r.put("data", t);
        return r;
    }

    public static R success(String msg) {
        R r = new R();
        r.put("message", msg);
        return r;
    }

    public static R success(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }

    /**
     * 自定义分页成功返回内容
     * @param page
     * @return
     */
    public static R success(IPage page) {
        R r = new R();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("data", PageInfo.getPageInfo(page));
        r.putAll(map);
        return r;
    }

    public static <T> R success(T t) {
        R r = new R();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("data", t);
        r.putAll(map);
        return r;
    }

    public static R success() {
        return new R();
    }

    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
